<?php

namespace app\service;

//玩家卡牌服务层

use app\model\Card;
use app\model\UserCard;

class UserCardService extends BaseService
{

    //添加玩家卡牌
    public function add_user_card($user_id, $card_id, $num): array
    {
        //查询卡牌是否存在
        $card = Card::where("id", $card_id)->find();
        if (!$card) {
            return $this->returnFail("卡牌不存在");
        }

        //查询玩家是否已经拥有该卡牌碎片
        $user_card = UserCard::where("user_id", $user_id)->where("card_id", $card_id)->find();
        if (!$user_card) {
            $user_card = new UserCard();
            $user_card->user_id = $user_id;
            $user_card->card_id = $card_id;
        }
        $user_card->num = $user_card->num + $num;
        $user_card->save();

        return $this->returnSuccess("添加成功");
    }

}